package com.joy.mms.controller;

import java.util.List;
import java.util.Arrays;

import lombok.RequiredArgsConstructor;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.joy.common.annotation.RepeatSubmit;
import com.joy.common.annotation.Log;
import com.joy.common.core.controller.BaseController;
import com.joy.common.core.domain.PageQuery;
import com.joy.common.core.domain.R;
import com.joy.common.core.validate.AddGroup;
import com.joy.common.core.validate.EditGroup;
import com.joy.common.enums.BusinessType;
import com.joy.common.utils.poi.ExcelUtil;
import com.joy.mms.domain.vo.MachineCategoryVo;
import com.joy.mms.domain.bo.MachineCategoryBo;
import com.joy.mms.service.MachineCategoryService;

/**
 * 设备分类
 *
 * @author Joy
 * @date 2024-12-08
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/dategory/machineCategory")
public class MachineCategoryController extends BaseController {

    private final MachineCategoryService machineCategoryService;

    /**
     * 查询设备分类列表
     */
    @SaCheckPermission("dategory:machineCategory:list")
    @GetMapping("/list")
    public R<List<MachineCategoryVo>> list(MachineCategoryBo bo) {
        List<MachineCategoryVo> list = machineCategoryService.queryList(bo);
        return R.ok(list);
    }

    /**
     * 导出设备分类列表
     */
    @SaCheckPermission("dategory:machineCategory:export")
    @Log(title = "设备分类", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(MachineCategoryBo bo, HttpServletResponse response) {
        List<MachineCategoryVo> list = machineCategoryService.queryList(bo);
        ExcelUtil.exportExcel(list, "设备分类", MachineCategoryVo.class, response);
    }

    /**
     * 获取设备分类详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("dategory:machineCategory:query")
    @GetMapping("/{id}")
    public R<MachineCategoryVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(machineCategoryService.queryById(id));
    }

    /**
     * 新增设备分类
     */
    @SaCheckPermission("dategory:machineCategory:add")
    @Log(title = "设备分类", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody MachineCategoryBo bo) {
        return toAjax(machineCategoryService.insertByBo(bo));
    }

    /**
     * 修改设备分类
     */
    @SaCheckPermission("dategory:machineCategory:edit")
    @Log(title = "设备分类", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody MachineCategoryBo bo) {
        return toAjax(machineCategoryService.updateByBo(bo));
    }

    /**
     * 删除设备分类
     *
     * @param ids 主键串
     */
    @SaCheckPermission("dategory:machineCategory:remove")
    @Log(title = "设备分类", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(machineCategoryService.deleteWithValidByIds(Arrays.asList(ids), true));
    }
}
